SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		Emilio Augusto Veras Ivanovich
-- Create date: 12/06/2011
-- Description:	Reseeding Identity on Call Table
-- =============================================
ALTER PROCEDURE [dbo].[PROCEDURE_RESEED_COMPLAINTS]
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
	-- Get Last Inserted Date on the Table
	DECLARE @CURRENT_ID BIGINT;
	DECLARE @LAST_ID BIGINT;
	SET @LAST_ID = (SELECT TOP 1 LEFT([ID], 8) FROM [COMPLAINTS] ORDER BY [INSERTED] DESC);
	SET @CURRENT_ID = (SELECT REPLACE(CONVERT(VARCHAR(10), GETDATE(), 111), '/', ''));
	-- If the Current Date is Newer than the Las Inserted Date then Reseed otherwise return;
	IF (@LAST_ID < @CURRENT_ID)
	BEGIN
		-- If yes, reseed 
		-- Statements
		SET @CURRENT_ID = @CURRENT_ID * 1000;
		DBCC CHECKIDENT (COMPLAINTS, reseed, @CURRENT_ID);
	END;
END